<?php

namespace haibao\jiepai\business;

use haibao\frame\data\exception\MongoException;
use \haibao\jiepai\data\Message as MessageData;
use \haibao\jiepai\model\data\messages\CmsMessagesPush as CmsMessagePushModel;

class Message extends Base{

	/**
	 * 消息推送类型
	 *
	 */
	public static $targetView = array(
			CmsMessagePushModel::TARGETVIEW_HOME =>'首页',
			CmsMessagePushModel::TARGETVIEW_TUIJIAN =>'推荐',
			CmsMessagePushModel::TARGETVIEW_MINE =>'我',
			CmsMessagePushModel::TARGETVIEW_ARTICLE =>'文章',
	);

    public static function getNoticeList($options){

        $data = new MessageData();
        return $data->getNoticeList($options);
    }
    
    public static function getPushList($options){
    
    	$data = new MessageData();
    	return $data->getPushList($options);
    }

    public static function addPush($model){
    	$data = new MessageData();
    	$data->add($model);
    	\haibao\jiepai\business\UPush::sendPush($model);
    }
    
    public static function getNoticeInfo($noticeId){

        $data = new MessageData();
        return $data->getNoticeInfo($noticeId);
    }

    public static function addNotice($model){

        $data = new MessageData();
        $data->add($model);

        // TODO : 推送消息
        self::pushManageCmsMsg($model,$data);
    }

    public static function updateNotice($noticeId, $arrSet){

        if(!$noticeId){
            throw new BusinessException('通知ID不存在');
        }

        $data = new MessageData();
        $data->updateById($noticeId,  $arrSet);
    }

    public static function pushManageCmsMsg($noticeModel, $messageData){

        if($noticeModel->RangeType == \haibao\jiepai\model\data\messages\CmsMessagesManage::RangeType_device){

            if(!$noticeModel->IsOnlyUser){
                $messageData->getVisitorIdAndSaveMessage(0, $noticeModel->Id, $noticeModel->HomeTopTitle);

            }
        }else{

            if($noticeModel->ToUserID){

                $topTitle = $noticeModel->HomeTopTitle?6:5;
                $sql = 'insert into cms_messages (UserID,TypeID,Type) values (%s,%s,%s)';
                $messageData->query($sql, array($noticeModel->ToUserID, $noticeModel->Id, $topTitle));
            }
        }
    }

    public static function getHomeTopMsg($userId, $vistorId){

        if(!$userId && !$vistorId) return NULL;

        $data = new MessageData();
        $msg = $data->getHomeTopMsg($userId, $vistorId);
        return $msg?$msg:null;

    }
    
    public static function countManageMessage($userId, $vistorId){
    	if(!$userId && !$vistorId) return NULL;
    	$data = new MessageData();
    	return $data->countManageMessage($userId,$vistorId);
    }
    
    public static function countCommentMessage($userId){
    	$data = new MessageData();
    	return $data->countCommentMessage($userId);
    }
    
    public static function countPraiseMessage($userId,$isOld){
    	$data = new MessageData();
    	return $data->countPraiseMessage($userId,$isOld);
    }
    public static function countPraiseMessagePicture($userId){
    	$data = new MessageData();
    	return $data->countPraiseMessagePicture($userId);
    }
    
    public static function countAdviceMessage($userId){
    	$data = new MessageData();
    	return $data->countAdviceMessage($userId);
    }

    public static function getUserIgnore($userId){

        $data = new MessageData();
        return $data->getUserIgnore((int)$userId);
    }

    public static function setUserIgnore($userId, $ignoreId, $type){

        $data = new MessageData();
        return $data->setUserIgnore((int)$userId, (int)$ignoreId, (int)$type);
    }

    public static function getHistoryManageMessages($userId, $visitorId=null, $cursor=null){

        $data = new MessageData();
        return $data->getHistoryManageMessages($userId, $visitorId, $cursor);
    }

    public static function getUnReadManageMessages($userId, $visitorId=null, $cursor=null, $stopCursor = null){

        $data = new MessageData();
        return $data->getUnReadManageMessages($userId, $visitorId, $cursor, $stopCursor);
    }

    //获取九点推送消息
    public static function getPushMessage(){

        $data = new MessageData();
        return $data->getPushMessage();
    }
    //设置首页置顶消息已读
    public static function readHomeTopMsg($userId=null, $visitorId=null){

        $data = new MessageData();
        return $data->readHomeTopMsg($userId, $visitorId);
    }

    // 获取评论历史消息
    public static function getHistoryCommentMessages($userId, $version='4.5.0', $cursor=null){

        $data = new MessageData();
        return $data->getHistoryCommentMessages((int)$userId,  $version, $cursor);
    }
    // 获取评论未读消息
    public static function getUnReadCommentMessages($userId, $version='4.5.0', $cursor=null, $stopCursor = null){

        $data = new MessageData();
        return $data->getUnReadCommentMessages((int)$userId,  $version,$cursor, $stopCursor);
    }

    //获取消息详情
    public static function getDetailByTypeIDAndType($typeId, $type){

        $data = new MessageData();
        return $data->getDetailByTypeIDAndType((int)$typeId, (int)$type);
    }

    //获取历史点赞消息
    public static function getHistoryPraiseMessages($userId, $version='4.5.0', $cursor=null){
        $data = new MessageData();
        return $data->getHistoryPraiseMessages((int)$userId, $version, $cursor);
    }
    //获取所有未读点赞消息（流水表）
    public static function getUnReadPraiseMessages($userId, $version='4.5.0', $cursor=null){
        $data = new MessageData();
        return $data->getUnReadPraiseMessages((int)$userId, $version, $cursor);
    }

    //获取点赞消息的点赞用户
    public static function getPraiseMessageUsers($messageId){

        $data = new MessageData();
        return $data->getPraiseMessageUsers((int)$messageId);
    }
    //新增点赞历史消息
    public static function insertPraiseMessage($userId, $key, $message){

        $data = new MessageData();
        return $data->insertPraiseMessage($userId, $key, $message);
    }

    //新增点赞历史消息
    public static function getPraiseMessageUsersByCursor($messageId, $cursor=null){

        $data = new MessageData();
        return $data->getPraiseMessageUsersByCursor($messageId, $cursor);
    }

    /**
     * 首次进入消息提示
     */
    public static function firstEntryMessage($userId)
    {
        $data = new MessageData();
        $data->firstEntryMessage($userId);
    }
}